

cd "/disk/agedisk3/medicare.work/chandra-DUA52080/pragya-dua52080/replication/"
************ SETUP *******************
set more off
capture log close
clear
version 14.0

log using figures.log, replace

set more off
set scheme s1color

** Min and max periods ** 
local minperiod = 1
local maxperiod = 4

** makes the density plot
global DENSITYPLOT = 1

** makes the assume starting period quality or market share
global DYNAMIC_PLOT = 1


if ($DENSITYPLOT == 1){	
	foreach period in `minperiod' `maxperiod'{
	
	** provider list
	use * if yidx == 1 | yidx == 4 using   Intermediate_Output_Not_Exportable/bs0.dta, clear
	** keep the balanced sample
	gen byte insamp = longsurv_ami_fere_a != . 
	bysort pn: egen num_times_appear = sum(insamp)
	keep if num_times_appear == 2	
	keep pn yidx longsurv_ami_fere_a
	tempfile provider_list
	save `provider_list'			
	
	** Pulls in individual data
	use race_alt yidx pn year zip_weight_w zip_weight_b hrrnum_bene hrrnum_weight_w hrrnum_weight_b if yidx == `period' using  Intermediate_Output_Not_Exportable/ami100.dta, replace
	
	** limit to providers in sample	
	merge m:1 pn yidx using `provider_list', keep(match) nogenerate
	
	** Rescales to *100
	replace longsurv_ami_fere_a  = longsurv_ami_fere_a*100
		
	** 	Captures mean, median, sd for excel and to create line on distribution plot ** 
		summarize longsurv_ami_fere_a if race_alt == 1 
		local mean_white = r(mean)

		summarize longsurv_ami_fere_a [aw=zip_weight_w] 
		local mean_white_wgt = r(mean)

		summarize longsurv_ami_fere_a [aw=hrrnum_weight_w] 
		local mean_white_wgtH = r(mean)
			
		summarize longsurv_ami_fere_a if race_alt == 2
		local mean_black = r(mean)

		summarize longsurv_ami_fere_a [aw=zip_weight_b] 
		local mean_black_wgt = r(mean)

		summarize longsurv_ami_fere_a [aw=hrrnum_weight_b] 
		local mean_black_wgtH = r(mean)
		
	
	if `period' == `minperiod' {

			capture noisily mkdir Exportable_Results/
			capture noisily mkdir Exportable_Results/Figure1/	
			capture noisily mkdir Exportable_Results/Figure2/
			** FIGURE 1 PANEL A
			** With legends	
			twoway kdensity longsurv_ami_fere_a if race_alt == 1, lcolor(black) range(82 96) xline(`mean_white', lcolor(black)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) || kdensity longsurv_ami_fere_a if race_alt == 2 ,  legend(order(1 "White patients" 2 "Black patients")) ytitle("Density", size(large)) xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(red*.3) xline(`mean_black', lcolor(red*.3) ) range(82 96) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) title(" " " A. Between ", ring(0) pos(10)) ysize(2.5) xsize(4) graphregion(margin(1 1 2 2)) ylabel(0(.05).2, tlength(0) angle(0) format(%03.2f) labsize(large)) 
			graph export  Exportable_Results/Figure1/Figure1_PanelA.pdf, replace
			

			** FIGURE 1 PANEL B
			** PLOT 2: plots distribution of quality measure weighted by HRR, adds to plot 1 **
			twoway 	kdensity longsurv_ami_fere_a if race_alt == 1, lcolor(black) range(82 96) xline(`mean_white', lcolor(black)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) || kdensity longsurv_ami_fere_a [aw=hrrnum_weight_w], lcolor(black) range(82 96) xline(`mean_white_wgtH', lcolor(black) lpattern(longdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(longdash) || kdensity longsurv_ami_fere_a if race_alt == 2,  legend(order(1 "White patients" 2 "HRR Reweighted")) ytitle("Density", size(large)) yla(0(.05).2, tlength(0) angle(0) format(%03.2f) labsize(large))  xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(black*0) range(82 96) xline(`mean_black', lcolor(black*0) lpattern(longdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(longdash) title(" " " B. Macro-Geography", ring(0) pos(10)) ysize(2.5) xsize(4) graphregion(margin(1 1 2 2))	 
			graph export  Exportable_Results/Figure1/Figure1_PanelB.pdf, replace
			
			
			** FIGURE 1 PANEL C
			** PLOT 3: plots distribution of quality measure weighted HRR & zip , adds to plot 2 (highlights HRR & zip) ** 
			twoway kdensity longsurv_ami_fere_a [aw=hrrnum_weight_w],  lcolor(black) range(82 96) xline(`mean_white_wgtH', lcolor(black) lpattern(longdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(longdash) || kdensity longsurv_ami_fere_a [aw=zip_weight_w], lcolor(black) range(82 96) xline(`mean_white_wgt', lcolor(black) lpattern(shortdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(shortdash) || kdensity longsurv_ami_fere_a if race_alt == 2, lcolor(black*0) range(82 96) xline(`mean_black', lcolor(black*0) lpattern(longdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(longdash) || kdensity longsurv_ami_fere_a if race_alt == 1, legend(order(1 "HRR Reweighted" 2 "Zip Reweighted")) ytitle("Density", size(large)) yla(0(.05).2, tlength(0) angle(0) format(%03.2f) labsize(large))  xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(black*0) range(82 96) xline(`mean_white', lcolor(black*0)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) title(" " " C. Micro-Geography", ring(0) pos(10)) ysize(2.5)  xsize(4) graphregion(margin(1 1 2 2)) 	
			graph export  Exportable_Results/Figure1/Figure1_PanelC.pdf, replace
			

			** FIGURE 1 PANEL D
			** PLOT 4: plots distribution of quality measure weighted HRR & zip , adds to plot 2 (highlights black vs. white zip weighted) ** 
			twoway 	kdensity longsurv_ami_fere_a [aw=zip_weight_w], lcolor(black) range(82 96) xline(`mean_white_wgt', lcolor(black) lpattern(shortdash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(shortdash) || kdensity longsurv_ami_fere_a if race_alt == 2, lcolor(red*.3) range(82 96) xline(`mean_black', lcolor(red*.3) ) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large))  || kdensity longsurv_ami_fere_a [aw=hrrnum_weight_w],  lcolor(black*0) range(82 96) xline(`mean_white_wgtH', lcolor(black*0) lpattern(dash)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) lpattern(dash) || kdensity longsurv_ami_fere_a if race_alt == 1,  legend(order(1 "Zip Reweighted" 2 "Black Patients")) ytitle("Density", size(large)) yla(0(.05).2, tlength(0) angle(0) format(%03.2f) labsize(large)) xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(black*0) range(82 96) xline(`mean_white', lcolor(black*0)) xlabel(82 "82%" 84 "84" 86 "86" 88 "88" 90 "90" 92 "92" 94 "94" 96 "96", labsize(large)) title(" " " D. Residual", ring(0) pos(10)) ysize(2.5)  xsize(4)  graphregion(margin(1 1 2 2))	
			graph export  Exportable_Results/Figure1/Figure1_PanelD.pdf, replace
			
			** FIGURE 2 PANEL A			
			twoway kdensity longsurv_ami_fere_a if race_alt == 1, lcolor(black) range(82 100) xline(`mean_white', lcolor(black)) xlabel(82 "82%" 85 "85" 88 "88" 91 "91" 94 "94" 97 "97" 100 "100", labsize(large)) || kdensity longsurv_ami_fere_a if race_alt == 2 ,  legend(off) ytitle("Density", size(large)) xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(red*.3) xline(`mean_black', lcolor(red*.3) ) range(82 96) xlabel(82 "82%" 85 "85" 88 "88" 91 "91" 94 "94" 97 "97" 100 "100", labsize(large)) title(" " " A. Baseline", ring(0) pos(10)) ysize(2.5) xsize(4) graphregion(margin(1 1 2 2)) ylabel(0(.05).2, angle(0) format(%03.2f) labsize(large)) 
			graph export  Exportable_Results/Figure2/Figure2_PanelA.pdf, replace
			
			
	}
	
	if `period' == `maxperiod'{

		capture noisily mkdir Exportable_Results/Figure2/		
		** FIGURE 2 PANEL B	
		twoway kdensity longsurv_ami_fere_a if race_alt == 1, lcolor(black) range(82 96) xline(`mean_white', lcolor(black)) xlabel(88 "88%" 90 "90" 92 "92" 94 "94" 96 "96" 98 "98" 100 "100", labsize(large)) || kdensity longsurv_ami_fere_a if race_alt == 2 ,  legend(order(1 "White Patients" 2 "Black Patients")) ytitle("Density", size(large)) xtitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large)) lcolor(red*.3) xline(`mean_black', lcolor(red*.3) ) range(82 96) xlabel(88 "88%" 90 "90" 92 "92" 94 "94" 96 "96" 98 "98" 100 "100", labsize(large))  title(" " " B. Endline", ring(0) pos(10)) ysize(2.5) xsize(4) graphregion(margin(1 1 2 2)) ylabel(0(.05).2, angle(0) labsize(large)) 
		graph export  Exportable_Results/Figure2/Figure2_PanelB.pdf, replace
		
	}
	
	
}

}


if $DYNAMIC_PLOT == 1 {

	local panel_list "A B C"
	foreach panel in `panel_list'{
	
	** Panel A: Dynamic plot without holding share or quality fixed at period 1 levels
		
	use  Intermediate_Output_Not_Exportable/bs0.dta, clear
	** keep the balanced sample
	gen byte insamp = longsurv_ami_fere_a != . 
	bysort pn: egen num_times_appear = sum(insamp)
	keep if num_times_appear == 2	
	keep pn npatients_b_ami_tot_act npatients_w_ami_tot_act yidx longsurv_ami_fere_a
			
	** Rescales to *100
	replace longsurv_ami_fere_a  = longsurv_ami_fere_a*100
	
	
		if "`panel'" == "B" {
			reshape wide npatients_b_ami_tot_act npatients_w_ami_tot_act longsurv_ami_fere_a, i(pn) j(yidx)
			foreach num of numlist 2/4{
				replace npatients_b_ami_tot_act`num' = npatients_b_ami_tot_act1
				replace npatients_w_ami_tot_act`num' = npatients_w_ami_tot_act1
			}
			reshape long npatients_b_ami_tot_act npatients_w_ami_tot_act longsurv_ami_fere_a, i(pn) j(yidx)
			local paneltitle1 "B. Fixing Market Shares at Baseline Level" 
		}
		if "`panel'" == "C" {
			reshape wide npatients_b_ami_tot_act npatients_w_ami_tot_act longsurv_ami_fere_a, i(pn) j(yidx)
			foreach num of numlist 2/4{
				replace longsurv_ami_fere_a`num' = longsurv_ami_fere_a1
			}
			reshape long npatients_b_ami_tot_act npatients_w_ami_tot_act longsurv_ami_fere_a, i(pn) j(yidx)
			local paneltitle1 "C. Fixing Performance at Baseline Level" 
		}
		
		generate product_black_quality = npatients_b_ami_tot_act*longsurv_ami_fere_a
		generate product_white_quality = npatients_w_ami_tot_act*longsurv_ami_fere_a
		collapse (sum) product_black_quality product_white_quality npatients_b_ami_tot_act npatients_w_ami_tot_act, by(yidx)
		generate black_quality = product_black_quality / npatients_b_ami_tot_act
		generate white_quality = product_white_quality / npatients_w_ami_tot_act
		generate year = 1995+5*(yidx-1)+2
		keep if year > 1995

		capture noisily mkdir Exportable_Results/Figure3/	
		if "`panel'" == "A"{
			local paneltitle1 "A. Hospital Performance by Race over Time" 
			twoway connected black_quality year, lcolor(red*.3) mcolor(red*.3) msymbol(circle) || connected white_quality year, lcolor(black) mcolor(black) msymbol(triangle)   ytitle("Hospital Performance" "(Survival, 30 Days, Risk-Adjusted)", size(large) height(10) ) legend(lab(1 "Black Patient Mean") lab( 2 "White Patient Mean") rows(2)) legend(size(large)) xlabel(1997 "1995-1999" 2002 "2000-2004" 2007 "2005-2009" 2012 "2010-2014", labsize(large)) graphregion(color(white))  title(" " "{it: `paneltitle1'}" , ring(0) pos(10)) ysize(3) xsize(3) graphregion(margin(medium))  yla(88 "88" 90 "90" 92 "92" 94 "94%", angle(0) labsize(large) nogrid) xtitle("") yscale(r(88 94.6))
			graph export  Exportable_Results/Figure3/Figure3_PanelA.pdf, replace
					
		}
		if "`panel'" != "A"{
			twoway connected black_quality year, lcolor(red*.3) mcolor(red*.3) msymbol(circle) || connected white_quality year, lcolor(black) mcolor(black) msymbol(triangle)   xlabel(1997 "1995-1999" 2002 "2000-2004" 2007 "2005-2009" 2012 "2010-2014", labsize(large)) graphregion(color(white))  ysize(3) xsize(3) graphregion(margin(2.5 2.5 11 1.6)) legend(off) ytitle("") yla(88 "88" 90 "90" 92 "92" 94 "94%", tlength(0) angle(0) labsize(large)) yscale(r(88 94.6) off) title(" " "{it: `paneltitle1'}", ring(0) pos(10)) xtitle("")
			graph export  Exportable_Results/Figure3/Figure3_Panel`panel'.pdf, replace
		}

	}
	


}



